import java.io.*;


public class nQueen {
	public static void main(String[] args){
		int n = Integer.parseInt(args[0]);
		
		int no_Clauses=0;
		//initializing the array
		int val =1;
		int [][] prop = new int[n+1][n+1];
		for(int i =1 ; i<=n;i++){
			for(int j=1;j<=n;j++){
				prop[i][j] = val;
				val++;
			//	System.out.println(prop[i][j]);
			}
		}
		
		//printing the CNF in temporary input_temp file;
		try{
			PrintWriter pw = new PrintWriter(new File("input_temp.txt"));
			
			
			//queen should be present in every row.
			pw.println("c queen should be present in every row.");
			for(int i =1;i<=n;i++){
				for(int j=1;j<=n;j++){
					pw.print(prop[i][j]+ " ");
				}
				pw.println("0");
				no_Clauses++;
			}
			
			
			
			for(int i =1 ; i<=n;i++){
				for(int j=1;j<=n;j++){
					//row constraint
					//pw.println("c row constraint for "+prop[i][j]);
					pw.println("");
					int k =j+1;
					while(k<=n){
						
							pw.print(-prop[i][j]+" ");
							pw.print(-prop[i][k]+" ");
							pw.println("0");
							no_Clauses++;
						
						
						k++;
					}
					
					
					//column constraint
					//pw.println("c column constraint for "+prop[i][j]);
					pw.println("");
					k = i+1;
					while(k<=n){
							pw.print(-prop[i][j]+" ");
							pw.print(-prop[k][j]+" ");
							pw.println("0");
							no_Clauses++;
					
						
						k++;
					}
					
					
					//diagonal constrain  in south east direction
					//pw.println("c diagonal constraint in south east direction for "+ prop[i][j]);
					pw.println("");
					int l = i+1;
					int m = j+1;
					while(l<=n & m<=n){
						pw.print(-prop[i][j]+" ");
						pw.print(-prop[l][m]+" ");
						pw.println("0");
						no_Clauses++;
						l++;
						m++;
					}
					
					//diagonal constraint in north west direction
					//pw.println("c diagonal constraint in south west direction for "+ prop[i][j]);
					pw.println("");
					l = i+1;
					m = j-1;
					while(l<=n & m>=1){
						pw.print(-prop[i][j]+" ");
						pw.print(-prop[l][m]+" ");
						pw.println("0");
						no_Clauses++;
						l++;
						m--;
					}
					
					
					
					
					
				}
			}
			
			pw.flush();
			pw.close();
		
			//making the header of input file
			PrintWriter pw2 = new PrintWriter(new File("CNF.txt"));
			pw2.println("c nQueen problem using knowledge representational logic and sat solver minisat");
			pw2.println("c solving for size "+ n+"*"+n);
			pw2.println("p CNF "+n*n+" "+no_Clauses);
				
			//copying contents from input_temp.txt
			BufferedReader br = new BufferedReader(new FileReader(new File("input_temp.txt")));
			String line;
			while((line = br.readLine())!=null){
				pw2.println(line);
			}
			br.close();
			
			pw2.flush();
			pw2.close();
			
			//deleting temporary file
			(new File("input_temp.txt")).delete();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}
